import 'package:flutter/material.dart';

main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "list demo",
      home: MyHomePage(
        title: "首页",
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool empty = false;
  final _controller = FixedExtentScrollController();
  final List<String> items = List.generate(100, (i) => "Item ${i + 1}");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: GestureDetector(
            child: Text(widget.title),
            onTap: () {
              if (!empty) {
                // animateTo动画跳转
                _controller.animateTo(-20,
                    duration: const Duration(seconds: 2), curve: Curves.ease);
              }
            },
          ),
        ),
        body: RefreshIndicator(
          onRefresh: () => Future.delayed(const Duration(seconds: 3)),
          child: empty
              ? ListView(
                  children: [
                    Container(
                      height: 400,
                      color: Colors.blue,
                      child: const Text("no data"),
                    ),
                  ],
                )
              : ReorderableListView(
                  itemExtent: 60,
                  onReorder: (oldIndex, newIndex) {
                    // print(oldIndex);
                    // print(newIndex);
                    setState(() {
                      //交换数据
                      if (newIndex > oldIndex) {
                        newIndex -= 1;
                      }
                      final item = items.removeAt(oldIndex);
                      items.insert(newIndex, item);
                    });
                  },
                  children: items
                      .map((e) => Container(
                            key: UniqueKey(),
                            alignment: Alignment.center,
                            color: Colors.blue,
                            child: Text(e),
                          ))
                      .toList()),
        ));
  }
}
